<?php
ob_start();
header('Content-Type: text/plain; charset=utf-8');

$w = @$_GET['w'];
$t = @$_GET['t'];

$w = file_get_contents($w);

// print_r($w);
// $xml = simplexml_load_string($w);

print_r(getTrad($w, 'ru'));












function getTrad($html, $lang = NULL) {
	$pos = strpos($html, 'p-lang');
	$pos = strpos($html, '<ul', $pos);
	$fin = strpos($html, '</ul>', $pos) + 5;
	$ul = substr($html, $pos, $fin - $pos);
	
	$xml = simplexml_load_string($ul);
	
	// var_dump($xml);
	
	foreach ($xml->li as $li) {
			// var_dump($li->a);
		if((string)$li->a['lang'] == $lang) {
			// var_dump($li->a['title'], $li->a['href']);
			list($titre) = explode('—', $li->a['title']);
			$trad = array(trim($titre), 'http:' . (string)$li->a['href']);
			break;
		}
	}
	
	
	
	return $trad;

}




function getMassif($xml, $massif = 'Massif') {
	foreach ($xml->body->div as $div) {
		if((string)$div['id'] == 'content') {
			break;
		}
	}
	// bodyContent
	foreach ($div->div as $div1) {
		if((string)$div1['id'] == 'bodyContent') {
			break;
		}
	}


	// mw-content-text
	foreach ($div1->div as $div2) {
		if((string)$div2['id'] == 'mw-content-text') {
			break;
		}
	}

	foreach($div2->table->tr as $tr) {
		if($tr->th->a == $massif) {
			// var_dump($tr->td);
			if(count($tr->td->a) > 1) {
				$m = ($tr->td->a[1]);
				
			} else {
				$m = ($tr->td->a[0]);
			}
			$lien = ((string)$m['href']);
			$nom = ((string)$m['title']);
			$ma = array('nom' => $nom, 'lien' => $lien);
			break;
		}
	}
	// $massif = $m;
	return $ma;
}


// var_dump($xml->body);



